
local w,h = 250,180
local ui = class.panel.create('',(1920-w)/2,650,w,h)
ui:set_level(99)
ui:hide()
ui.back = ui:add_panel([[textures\black32.blp]],0,0,w,h)
ui.title = ui:add_text('升级词条',0,5,nil,30,14,5)
ui.back:set_alpha(0.5)

local w1 = 200
local h1 = 30
ui.buttons = {}
for a=1,4 do
    local button = ui:add_button([[textures\black32.blp]],(w-w1)/2,40 + (a-1)*(h1+5),w1,h1,1)
    button.index = a
    button.title = button:add_text('⑧物理伤害+10%',0,0,nil,nil,12,5)
    button.color = 0xff00ff00
    ui.buttons[a] = button
end

function ui:on_button_mouse_enter(button)
    button.title:set_color(button.color)
end

function ui:on_button_mouse_leave(button)
    button.title:set_color(0xffffffff)
end

function ui:on_button_clicked(button)
    ac.sync('同步-选择升级词条',button.index)
    self:hide()
end

function ui:set_data(data)
    for i,button in ipairs(self.buttons) do
        local temp = data[i]
        if temp then
            button.title:set_text(temp.title or temp.name or temp[1])
            button:show()
            if button:has_mouse() then
                button.title:set_color(button.color)
            else
                button.title:set_color(0xffffffff)
            end
        else
            button:hide()
        end
    end
    self.back:set_height(#data*35 + 45)
    self:show()
end


ac.sync:event '同步-选择升级词条'(function(_,player,index)
    local info = player:get_data('词条选择内容')
    local list = player:get_data('词条内容队列') or {}
    local data = info and info[1]
    local func = info and info[2]
    local button = data[index]
    if list[1]==info then
        table.remove(list,1)
    end

    if func and button then
        func(button,index)
    end

    player:notify('玩家-显示升级词条',player)
end)

ac.game:event '玩家-显示升级词条'(function(_,player)
    local list = player:get_data('词条内容队列') or {}
    local data = list[1]
    if not data then
        return
    end
    local info = data[1]
    player:set_data('词条选择内容',data)
    if type(info)=='function' then
        data[1] = info()
    end
    if player:is_self() then
        ui:set_data(data[1])
        ui.title:set_text(('升级词条(|cff00ff00%s|r)'):format(#list))
    end
end)

ac.game:event '玩家-添加升级词条'(function(_,player,data,func)
    local list = player:get_data('词条内容队列') or {}
    table.insert(list,{data,func})
    player:set_data('词条内容队列',list)
    player:notify('玩家-显示升级词条',player)
end)



-- local data = {
--     {title = '⑧物理伤害+10%', color = 0xff00ff00},
--     {title = '⑥法术伤害+10%', color = 0xff00ff00},
--     {title = '⑦生命值+1000', color = 0xff00ff00},
--     {title = '⑨攻击速度+10%', color = 0xff00ff00}
-- }
-- local player = ac.player[1]
-- player:notify('玩家-添加升级词条',player,data)
-- player:notify('玩家-添加升级词条',player,data)
-- player:notify('玩家-添加升级词条',player,data)
--ui:set_data(data)
